import { _decorator, Component, Node, AudioSource, AudioClip, Game, Layout } from 'cc';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
import { Enum_AudioClip } from '../Enum';
import { ResourcesUtil } from '../ResourcesUtil';
const { ccclass, property } = _decorator;

@ccclass('HeroSelectPanel')
export class HeroSelectPanel extends Component {

    @property(Node) rolesNode: Node = null;
    @property(Layout) layout: Layout = null;

    private _audioSound: AudioSource;
    private _enroll: AudioClip;

    private teamIndex1PosX: number = 0;
    private teamIndex2PosX: number = 0;

    async onLoad(): Promise<void> {
        // 音频
        this._enroll = await ResourcesUtil.loadAudioClip(Enum_AudioClip.enroll);
        this._audioSound = this.node.getComponent(AudioSource);
        this._audioSound.playOneShot(this._enroll);
    }

    start() {
        GameAPI.RegGEvent(EnEvent.ClickHeroItemEvent, this.OnClickHeroItemEvent.bind(this), this);
        this.layout.enabled = true;
        this.teamIndex1PosX = this.rolesNode.getChildByName("HeroPanelItem1").position.x;
        this.teamIndex2PosX = this.rolesNode.getChildByName("HeroPanelItem2").position.x;
    }

    onEnable() {
        let count = 0;
        for (let index = 0; index < this.rolesNode.children.length; index++) {
            const element = this.rolesNode.children[index];
            if (count >= 2) {
                break;
            }
            else {
                // if (count == 1) {
                //     element.setPosition(this.teamIndex1PosX, element.position.y);
                // }
                // else if (count == 2) {
                //     element.setPosition(this.teamIndex2PosX, element.position.y);
                // }
                element.active = true;
            }
            count = count + 1;
        }
        if (this._audioSound) {
            this._audioSound.playOneShot(this._enroll);
        }
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnClickHeroItemEvent(arg) {
        for (let index = 0; index < this.rolesNode.children.length; index++) {
            const element = this.rolesNode.children[index];
            if (arg == element.name) {
                element.destroy();
                break;
            }
        }
        GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 4, active: false });
        this.node.active = false;
        if (arg == "HeroPanelItem1") {
            GameAPI.FireGEvent(EnEvent.ShowTeamEvent, 1);
        }
        else if (arg == "HeroPanelItem2") {
            GameAPI.FireGEvent(EnEvent.ShowTeamEvent, 2);
        }
        else if (arg == "HeroPanelItem3") {
            GameAPI.FireGEvent(EnEvent.ShowTeamEvent, 3);
        }
        else if (arg == "HeroPanelItem4") {
            GameAPI.FireGEvent(EnEvent.ShowTeamEvent, 4);
        }
        else if (arg == "HeroPanelItem5") {
            GameAPI.FireGEvent(EnEvent.ShowTeamEvent, 5);
        }
    }

}


